package com.zhangsheng.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.zhangsheng.annotation.NoRepeat;
import com.zhangsheng.api.CommonResult;
import com.zhangsheng.entity.model.Order;
import com.zhangsheng.sentinel.block.MysqlServiceBlock;
import com.zhangsheng.sentinel.fallback.MysqlServiceFallBack;
import com.zhangsheng.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 张晟
 * @version 1.0
 * @date 2022/11/22 15:09
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/get/{id}")
    @SentinelResource(value = "getOrderById" ,
            blockHandlerClass = MysqlServiceBlock.class ,
            blockHandler = "getOrderByIdBlock" ,
            fallbackClass = MysqlServiceFallBack.class ,
            fallback = "getOrderByIdFallback")
    public CommonResult<Order> getOrderById(@PathVariable("id") Integer orderId){
        Order order = orderService.selectById(orderId);
        if (order == null){
            return CommonResult.failed("订单不存在");
        }
        return CommonResult.success(order);
    }

    @NoRepeat
    @PostMapping("/create")
    @SentinelResource(value = "create" ,
            blockHandlerClass = MysqlServiceBlock.class ,
            blockHandler = "createOrderBlock" ,
            fallbackClass = MysqlServiceFallBack.class ,
            fallback = "createOrderFallback")
    public CommonResult<Order> create(@RequestBody Order order){
        log.info("创建订单");
        boolean result = orderService.createOrder(order);
        if (result){
            return CommonResult.success(order);
        }
        return CommonResult.failed("新增订单失败");
    }

    @PostMapping("/close")
    @SentinelResource(value = "close" ,
            blockHandlerClass = MysqlServiceBlock.class ,
            blockHandler = "closeOrderBlock" ,
            fallbackClass = MysqlServiceFallBack.class ,
            fallback = "closeOrderFallback")
    public CommonResult<Boolean> close(@RequestBody Order order){
        boolean result = orderService.closeOrder(order);
        if (result){
            return CommonResult.success(true);
        }
        return CommonResult.failed("取消订单失败");
    }

    @GetMapping("/slave1/getByPage")
    @SentinelResource(value = "getOrderByPage" ,
            blockHandlerClass = MysqlServiceBlock.class ,
            blockHandler = "getByPageBlock" ,
            fallbackClass = MysqlServiceFallBack.class ,
            fallback = "getByPageFallback")
    public CommonResult<List<Order>> getByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum ,
                                               @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){

        List<Order> orders = orderService.selectByPage(pageNum, pageSize);
        if (orders != null){
            return CommonResult.success(orders);
        }
        return CommonResult.failed("查询slave1商品失败");
    }

    @PostMapping("/rest")
    public CommonResult rest(HttpServletRequest request, @RequestBody Order order){
        String authorization = request.getHeader("Authorization");
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", authorization);
        HttpEntity httpEntity = new HttpEntity(order, headers);
        ResponseEntity<Object> response = restTemplate.postForEntity("http://localhost:9001/mysql/order/create", httpEntity, Object.class);
        log.info("调用第三方接口返回状态码：{}",response.getStatusCode());
        log.info("调用第三方接口返回数据：{}",response.getBody());
        if (response.getStatusCode() != HttpStatus.OK){
            return CommonResult.failed("调用失败");
        }
        return CommonResult.success(response.getBody());
    }

}

